package com.jonniespratley.flexdatabasemanager.utils
{
	import com.adobe.utils.DateUtil;
	import com.adobe.utils.NumberFormatter;
	
	import mx.charts.ChartItem;
	import mx.charts.chartClasses.Series;
	import mx.charts.series.BarSeries;
	import mx.charts.series.items.BarSeriesItem;
	import mx.controls.dataGridClasses.DataGridColumn;
	
	public class FlexDataManagerUtils
	{
		public function FlexDataManagerUtils()
		{
		}
		
		static public function dgDate_LabelFunction( item:Object, column:DataGridColumn ):String
		{
			var newDate:String;
			
			if ( item.data is Date )
			
				var oldDate:Date = new Date( item.data );
				var m:String = DateUtil.getShortMonthName( oldDate );
				var d:String = DateUtil.getShortDayName( oldDate );
				var y:String = DateUtil.getShortYear( oldDate );

			
			return newDate.concat( m ).concat( d ).concat( y );
		}
		
		static public function dgDataLength_LabelFunction( item:Object, column:DataGridColumn ):String
		{
			var byteUtil:ByteUtils = new ByteUtils();
			var formatted:String = byteUtil.format( item.aDataLength, ByteUtils.KBYTES, 2 );
				
			return formatted;
		}
		
		static public function dgIndexLength_LabelFunction( item:Object, column:DataGridColumn ):String
		{
			var byteUtil:ByteUtils = new ByteUtils();
			var formatted:String = byteUtil.format( item.aIndexLength, ByteUtils.KBYTES, 2 );
				
			return formatted;
		}
		
		static public function dgMaxDataLength_LabelFunction( item:Object, column:DataGridColumn ):String
		{
			var byteUtil:ByteUtils = new ByteUtils();
			var formatted:String = byteUtil.format( item.aMaxDataLength, ByteUtils.MBYTES, 2 );
				
			return formatted;
		}
		
		static public function dgAvgDataLength_LabelFunction( item:Object, column:DataGridColumn ):String
		{
			var byteUtil:ByteUtils = new ByteUtils();
			var formatted:String = byteUtil.format( item.aAvgRowLength, ByteUtils.KBYTES, 2 );
				
			return formatted;
		}
		
		static public function chartDataLength_LabelFunction( element:ChartItem, series:Series ):String
		{
        	var item:BarSeriesItem = BarSeriesItem( element );
        	var ser:BarSeries = BarSeries( series );
        	
        	var byteUtil:ByteUtils = new ByteUtils();
			var formatted:String = byteUtil.format( item.xValue.Data_length, ByteUtils.KBYTES, 2 );
				
        	return formatted;
		}
		
		static public function chartData_LabelFunction( series:Series, item:Object, fieldName:String ):Object 
		{
			var byte:ByteUtils = new ByteUtils();
			
        	if( fieldName == "name" && series.id == "tableSize" )
        	{	  
            	return( item.data_length = byte.format( item.Data_length, ByteUtils.KBYTES, 2 ) );        		
        	} 
        	else if( fieldName == "yValue" && series.id=="lowClose" ) 
        	{
            	return( item.close.Low );        		
        	} 
        	else if( fieldName == "xValue" ) 
        	{
            	return( item.month );        		
        	} 
        	else
        		
            return null;
  			            
      	}
      	
		/**
		 * Formats a MySQL Date to a readable date.
		 * 
		 * 0 - 2
		 * 1 - 0
		 * 2 - 0
		 * 3 - 9
		 * 4 - -
		 * 5 - 0
		 * 6 - 1
		 * 7 - -
		 * 8 - 2
		 * 9 - 1
		 * 10 - 
		 * 11 - 0
		 * 12 - 2
		 * 13 - :
		 * 14 - 4
		 * 15 - 7
		 * 16 - :
		 * 17 - 1
		 * 18 - 6
		 * 19 - 
		 * 
		 * 
		 * 
		 * example:
		 * in -> 2009-01-21 02:47:16
		 * out -> 1.21.2009
		 * 
		 * @param oldDate the old date string
		 * @param dateOrTime Either date or time
		 * @param seperator . / or -
		 * @return 
		 * 
		 */      	
		static public function formatMySQLDate( oldDate:String, dateOrTime:String, seperator:String = '.' ):String
		{
			var newDateTime:String = '';
			
			if ( oldDate != null )
			{
				var theYear:String = oldDate.substring( 0, 4 );
				var theMonth:String = oldDate.substring( 5, 7 );
				var theDay:String = oldDate.substring( 8, 10 )
				var theTime:String = oldDate.substring( 11, 19 );
				var theHours:String = theTime.substring( 0, 2 );
				var theMinutes:String = theTime.substring( 3, 5 );
				var theSeconds:String = theTime.substring( 6, 8 );			
				
				switch( dateOrTime )
				{
					case 'date':
						newDateTime = theMonth + seperator + theDay + seperator + theYear;
					break;
					
					case 'time':
						newDateTime = theHours + seperator + theMinutes;
					break;
				}	
			}
			
			return newDateTime;
		}

		/**
		 * Converts a date to a mysql timestamp, like 20081210152105 * month is 1-indexed
		 * 
		 * @param d
		 * @return 
		 * 
		 */		
		static public function toMysqlTimeStamp( d:Date ):String 
		{ 
			var sb:String = new String(); 
				sb += d.getFullYear();
				 
				sb += NumberFormatter.addLeadingZero( d.getMonth() + 1 ); 
				sb += NumberFormatter.addLeadingZero( d.getDate() ); 
				sb += NumberFormatter.addLeadingZero( d.getHours() ); 
				sb += NumberFormatter.addLeadingZero( d.getMinutes() ); 
				sb += NumberFormatter.addLeadingZero( d.getSeconds() ); 
				
			return sb; 
		} 


	}
}